/**
 * 第三场景 ScreenThree
 */
class ScreenThree extends egret.DisplayObjectContainer {
    /** 加载背景 */
    private bg_png = Main.createBitmapByName("bg_3_png");
    /** 加载小刀 */
    private dor_png = Main.createBitmapByName("dor_png");
    /** 加载报警器 */
    private bibibi_png = Main.createBitmapByName("bibibi_png");
    /** 加载角色 */
    private role_png = Main.createBitmapByName("role_3_0_png");
    /** 创建提示点 */
    private tipPoint = Main.createTipPoint();
    /** 创建bibibi提示点 */
    private tipPoint1 = Main.createTipPoint();
    /** 角色原地站立动画 */
    private role_timer: egret.Timer;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        Main.createScale(this.bg_png, this);
        Main.createScale(this.role_png, this);
        Main.createScale(this.dor_png, this);
        Main.createScale(this.bibibi_png, this);

        Main.anchorCenter(this.dor_png);
        this.dor_png.x = 343;
        this.dor_png.y = 267;
        this.dor_png.scaleX = .6;
        this.dor_png.scaleY = .6;

        Main.anchorCenter(this.bibibi_png);
        this.bibibi_png.x = 635;
        this.bibibi_png.y = 250;
        this.bibibi_png.scaleX = .6;
        this.bibibi_png.scaleY = .6;

        this.tipPoint.x = 660;
        this.tipPoint.y = 347;

        this.tipPoint1.x = 829;
        this.tipPoint1.y = 350;

        this.roleAnimation();

        Main.itemAniamtion(this.dor_png);

        Main.dragGetPatn(this.dor_png, this, {
            startFun: this.startDragDor,
            moveFun: this.moveDragCut
        });

        // Main.screenTitle(this, "第三幕");
        ScreenLayer.creatOpenAlert(this, 3);
    }
    /** 播放角色站立动画 */
    private roleAnimation() {
        let num = 0;
        this.role_timer = new egret.Timer(1000 / 2);
        this.role_timer.addEventListener(egret.TimerEvent.TIMER, () => {
            num++;
            if (num > 1) num = 0;
            this.role_png.texture = RES.getRes(`role_3_${num}_png`);
        }, this);
        this.role_timer.start();
    }

    /** 播放切断绳子动画 */
    private cutTheLine() {
        this.role_timer.stop();
        const timer = new egret.Timer(1000 / 3, 3);
        let num = 1;
        timer.addEventListener(egret.TimerEvent.TIMER, () => {
            num++;
            this.role_png.texture = RES.getRes(`role_3_${num}_png`);
        }, this);
        /** 动画播放完后启动报警器 */
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {
            Main.itemAniamtion(this.bibibi_png);
            Main.dragGetPatn(this.bibibi_png, this, {
                startFun: this.startDragBibibi,
                moveFun: this.moveDragBibibi
            });
        }, this);
        timer.start();
    }
    /** 拖动小刀开始 */
    private startDragDor(target: egret.Bitmap, container) {
        egret.Tween.removeTweens(target);
        /** 创建提示点 */
        Main.createScale(container.tipPoint, container);
        target.scaleX = 1;
        target.scaleY = 1;
        target.rotation = 90;
    }
    /** 拖动小刀中 */
    private moveDragCut(target: egret.Bitmap, container) {
        if (!target.hitTestPoint(container.tipPoint.x, container.tipPoint.y)) return;
        container.removeChild(container.dor_png);
        container.removeChild(container.tipPoint);
        container.cutTheLine();
    }
    /** 拖动报警器创建提示点 */
    private startDragBibibi(target: egret.Bitmap, container) {
        egret.Tween.removeTweens(target);
        target.scaleX = 1;
        target.scaleY = 1;
        /** 创建提示点 */
        Main.createScale(container.tipPoint1, container);
    }
    /** 拖动报警器  */
    private moveDragBibibi(target: egret.Bitmap, container) {
        if (!target.hitTestPoint(container.tipPoint1.x, container.tipPoint1.y)) return;
        container.role_png.texture = RES.getRes(`role_3_5_png`);
        container.removeChild(container.bibibi_png);
        container.removeChild(container.tipPoint1);
        container.jcRun();
    }
    /** 警察来 */
    private jcRun() {
        /** 角色动画定时器 */
        const timer = new egret.Timer(1000 / 3, 4);
        const run_png = Main.createBitmapByName(`JC1_png`);
        this.addChild(run_png);

        run_png.scaleX = 1.1;
        run_png.scaleY = 1.1;

        run_png.y = run_png.y - 50;

        let num = 1;
        timer.addEventListener(egret.TimerEvent.TIMER, () => {
            num++;
            run_png.texture = RES.getRes(`JC${num}_png`);
        }, this);

        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, () => {
            run_png.x = run_png.x + 100;
            egret.Tween.get(this).wait(2000).call(() => {
                this.over();
            })
        }, this);

        timer.start();
    }

    /** 完成 */
    private over() {
        const miva = this;
        const over_png = Main.createBitmapByName("over_png");
        const over_light_1 = Main.createBitmapByName("over_light_1_png");
        const over_light_2 = Main.createBitmapByName("over_light_2_png");

        center(over_light_2);
        center(over_light_1);
        center(over_png);

        over_light_2.alpha = 0;

        over_png.touchEnabled = true;
        over_png.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            this.dispatchEvent(new egret.Event("next"));
        }, this);

        egret.Tween.get(over_light_1, { loop: true }).to({ alpha: 0 }, 500).wait(500).to({ alpha: 1 }, 500);
        egret.Tween.get(over_light_2, { loop: true }).to({ alpha: 1 }, 500).wait(500).to({ alpha: 0 }, 500);

        function center(item) {
            Main.anchorCenter(item);
            item.x = miva.stage.stageWidth * .5;
            item.y = miva.stage.stageHeight * .5;
            miva.addChild(item);
        }

    }
}